<template>
  <div class="breadcrumb">
    <!-- 导航栏部分 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <!-- 默认样式紧挨着，这里的el-breadcrumb设置在全局样式文件 global.css中 -->
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片主体区域 -->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-row :gutter="30" class="mianbaoxie">
        <!-- :gutter="10"设置内部el-col之间的边距 -->
        <el-col :span="8">
          <!-- clearable ：多个可一键清空的小图标     @clear：点击一键清空后触发的事件-->
          <el-input
            placeholder="请输入内容"
            v-model="goodParams.query"
            clearable
            @clear="getGoodList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4"
          ><el-button type="primary" @click="goAddPage()"
            >添加商品</el-button
          ></el-col
        >
      </el-row>
      <el-table :data="goodList" border stripe>
        <!-- border：显示边框   stripe：隔行变色 -->
        <el-table-column type="index" lable="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="120px"> </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="90px"> </el-table-column>
        <el-table-column label="创建时间" width="170px"> 
          <template slot-scope="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="250px">
          <!-- size设置按钮大小 -->
          <!-- 通过 template来定义scope，通过其可以获取 row当前行信息，然后.mg_state就是用户状态的参数，设置到 switch 上就能显示状态了 -->
          <template slot-scope="scope">
            <el-button
              class="el-icon-edit-outline"
              type="primary"
              size="mini"
              @click="editGoodDialog(scope.row)"
            ></el-button>
            <el-button
              class="el-icon-delete-solid"
              type="danger"
              size="mini"
              @click="deleteGoodById(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页部分 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="goodParams.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="goodParams.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      goodList: [],
      total: 0,
      goodParams: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
    };
  },
  created() {
    this.getGoodList();
  },
  methods: {
    async getGoodList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.goodParams,
      });
      if (res.meta.status != 200) return this.$messgae.error(res.meta.msg);
      this.goodList = res.data.goods;
      this.total = res.data.total;
    },
    // 改变页码
    handleSizeChange(newSize) {
      this.goodParams.pagesize = newSize;
      this.getGoodList();
    },
    // 改变页数
    handleCurrentChange(newPage) {
      this.goodParams.pagenum = newPage;
      this.getGoodList();
    },
    goAddPage(){
      this.$router.push("/add");
    }
  },
};
</script>

<style scoped lang="less">
</style>
